#!/bin/bash
# - group

. $LKP_SRC/lib/reproduce-log.sh
. $LKP_SRC/lib/tests/version.sh
. $LKP_SRC/lib/detect-system.sh

arch=$(get_system_arch)

if [[ "$group" = "group-00" ]]; then
	test_array=("bitfield_kunit" "test_bits" "cmdline_kunit" "ext4-inode-test" "test_hash" \
	            "test_linear_ranges" "list-test" "memcpy_kunit" "dev_addr_lists_test" "resource_kunit" \
	            "lib_test" "sysctl-test" "time_test" "test_list_sort" "test_sort" \
	            "mptcp_crypto_test" "mptcp_token_test" "rational-test" "slub_kunit" \
	            "apparmor_policy_unpack_test" "fortify_kunit" "property-entry-test" \
	            "drm_cmdline_parser_test" "drm_damage_helper_test" "drm_dp_mst_helper_test" "drm_mm_test" \
	            "drm_format_helper_test" "drm_format_test" "drm_framebuffer_test" "drm_plane_helper_test" "drm_rect_test"\
	            "clk-gate_test" "clk_test" "cpumask_kunit" "cros_kunit_proto_test" "hid-uclogic-test" "iio-test-format" \
	            "iio-test-rescale" "overflow_kunit" "soc-utils-test" "stackinit_kunit" "is_signed_type_kunit" \
	            "siphash_kunit" "soc-topology-test" "fat_test" "string_kunit" "string_helpers_kunit" "usercopy_kunit")
elif [[ "$group" = "group-01" ]]; then
	test_array=("test_div64" "test_bpf" "test_min_heap" "test_static_key_base" \
	            "test_scanf" "test_bitmap" "test_uuid" "test_xarray" "test_rhashtable" "test_memcat_p" "dmabuf_selftests" "test_kprobes" "dmapool_test" \
	            "test_udelay" "test_klp_livepatch" "test_klp_shadow_vars" "test_hmm" "test_free_pages" "test_fpu" "test_rslib" "ntb_msi_test" "kprobe_event_gen_test" \
	            "test_bitops" "test_async_driver_probe" "spi-loopback-test" "globtest" "test_ref_tracker" "test_lockup" "scftorture" \
		        "preemptirq_delay_test" "test_blackhole_dev" "find_bit_benchmark" \
	            "test_kstrtox" "test_overflow" "test_ida" "udelay_test" "utf8-selftest" "test_dhry" "synth_event_gen_test"  \
	            "test_vmalloc" "test_klp_state" "test_meminit" "test_printf" "test_maple_tree" "thunderbolt_dma_test" "clocksource-wdtest" \
	            "crc32test" "atomic64_test" "snd-soc-avs-i2s-test" "test_dynamic_debug" "raid6test" "backtracetest")
elif [[ "$group" = "group-03" ]]; then
	test_array=("drm_buddy_test" "kasan_test" "kfence_test")
fi

run_kunit_tests()
{
	local kernel_version=$(get_kernel_version)

	for i in ${test_array[@]}; do
		echo "$FUNCNAME: test $i"

		# skip x86 only modules
		[[ $arch = "i386" ]] && {
			# TEST_LIVEPATCH
			[[ $i = test_klp_livepatch ]] && continue
			[[ $i = test_klp_shadow_vars ]] && continue
			# TEST_HMM
			[[ $i = test_hmm ]] && continue
		}

		modprobe -q -n $i || {
			echo "fail to modprobe -q -n $i on $kernel_version" >&2
			continue
		}

		log_cmd modprobe -v $i
		log_cmd rmmod $i
	done
}

run_kunit_tests
dmesg > "$TMP_RESULT_ROOT"/kunit
